package cn.xiqi.cms.hibernate.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.xiqi.cms.hibernate.DAO.RoleDAO;
import cn.xiqi.cms.hibernate.beans.Roleinfo;

public class RoleServiceImpl implements RoleService {
	private RoleDAO dao;

	@Override
	public int add(Roleinfo record) {
		int id = dao.insert(record);
		return id;
	}

	@Override
	public boolean delete(Roleinfo record) {
		boolean b = dao.delete(record);
		return b;
	}

	@Override
	public boolean changename(int id, String name) {
		boolean b;
		Roleinfo record;
		
		record = getByID(id);
		record.setName(name);
		b = dao.update(record);
		return b;
	}

	@Override
	public boolean setPower(int id, String power) {
		boolean b;
		Roleinfo record;
		
		record = getByID(id);
		record.setPower(power);
		b = dao.update(record);
		return b;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<Roleinfo> getAll() {
		List<Roleinfo> records;
		Map values = new HashMap<>();
		
		values.put("type", "all");
		records = dao.select(values);
		return records;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public Roleinfo getByID(int id) {
		Roleinfo record;
		Map values = new HashMap<>();
		
		values.put("type", "id");
		values.put("id", id);
		record = dao.select(values).get(0);
		return record;
	}

	@Override
	public String getPower(int id) {
		String power;
		Roleinfo record;
		
		record = getByID(id);
		power = record.getPower();
		return power;
	}

	@Override
	public void setDao(RoleDAO dao) {
		this.dao = dao;		
	}

}
